import { Component, OnInit, Input, EventEmitter, Output } from '@angular/core';
import { _HttpClient } from '@delon/theme';
import { environment } from '@env/environment';
import { DatePipe } from '@angular/common';
import { NzMessageService, UploadFile } from 'ng-zorro-antd';

@Component({
    selector: 'app-zdrwgzqd-jindu',
    templateUrl: './zdrwgzqd_jindu.component.html',
    styleUrls: ['./zdrwgzqd_jindu.component.less']
})

export class ZdrwgzqdJinduComponent implements OnInit {
    fileList = [];
    saveParam: any = {};
    pId: any;
    labelSpan = '6';
    contentSpan = '18';
    fontSize = '14px';
    @Output() optSuccess = new EventEmitter();
    @Output() optCancel = new EventEmitter();

    @Input() set params(param) {
        if (param.pId) {
            this.pId = param.pId;
        }
        this.initSaveParam();

    };
    @Input() set sizeMode(v) {
        this.labelSpan = v === 'max' ? '4' : '6';
        this.contentSpan = v === 'max' ? '20' : '18';
        this.fontSize = v === 'max' ? '20px' : '14px';
    }
    constructor(
        private http: _HttpClient,
        public message: NzMessageService,
        private datePipe: DatePipe,
    ) { }

    initSaveParam() {
        this.saveParam = { _sysDate: new Date() };
        this.fileList = [];
        if (this.pId) {
            this.getLastInfo(this.pId).subscribe(res => {
                if (!res.code) {
                    Object.assign(this.saveParam, res.data[0]);
                    if (this.saveParam.fileUrl) {
                        this.fileList = [{ uid: '', name: this.saveParam.fileName, url: this.saveParam.fileUrl }];
                    }
                }
            });
        }
    }

    ngOnInit(): void {

    }

    beforeUpload = (file: UploadFile): boolean => {
        this.fileList.splice(0, 1);
        this.fileList = this.fileList.concat(file);
        return false;
    };

    /**
     * 删除私立字段
     * @param param 1
     */
    removePrivateFields(param) {
        Object.keys(param).forEach(key => {
            if (!(key.indexOf('_')) || !param[key]) {
                delete param[key];
            }
        });
    }


    save() {
        const formData = new FormData();
        this.fileList.forEach((file: any) => {
            formData.append('file', file);
        });
        const sysDate = this.datePipe.transform(this.saveParam._sysDate, 'yyyy-MM-dd HH:mm');
        formData.append('pId', this.pId);
        formData.append('lastWeek', this.saveParam.lastWeek);
        formData.append('totalComplete', this.saveParam.totalComplete);
        formData.append('thisWeek', this.saveParam.thisWeek);
        formData.append('difficulty', this.saveParam.difficulty);
        if (sysDate) {
            formData.append('sysDate', sysDate);
        }
        this.add(formData).subscribe(res => {
            if (res.msg === 'Ok') {
                this.message.success('保存成功');
                this.optSuccess.emit();
                this.cancel();
            } else {
                this.message.warning('保存失败');
            }
        })
    }


    cancel() {
        this.optCancel.emit();
    }


    lrsjChange(newDate) {
        this.saveParam.time = this.datePipe.transform(newDate, 'yyyy-MM-dd HH:mm');
    }

    /**
     * 增加
     * @param pId 外键
     * @param lastWeek 上周情况
     * @param totalComplete 累积情况
     * @param thisWeek 这周情况
     * @param difficulty 存在困难
     * @param file 附件
     */
    add(param) {
        const dataurl = environment.problem + '/keyTaskProgress/add';
        return this.http.post(dataurl, param);
    }

    getLastInfo(pId) {
        const dataurl = environment.problem + '/keyTaskProgress/queryLast/' + pId;
        return this.http.get(dataurl);
    }
}
